package com.whl.order.service.impl;

import com.whl.basic.util.LoginContext;
import com.whl.basic.util.PageList;
import com.whl.order.domain.OrderPetAcquisition;
import com.whl.order.dto.OrderDto;
import com.whl.order.mapper.OrderPetAcquisitionMapper;
import com.whl.order.query.OrderPetAcquisitionQuery;
import com.whl.order.service.IOrderPetAcquisitionService;
import com.whl.basic.service.impl.BaseServiceImpl;
import com.whl.org.domain.Employee;
import com.whl.org.domain.Shop;
import com.whl.org.mapper.EmployeeMapper;
import com.whl.org.mapper.ShopMapper;
import com.whl.pet.domain.Pet;
import com.whl.pet.domain.SearchMasterMsg;
import com.whl.pet.mapper.PetMapper;
import com.whl.pet.mapper.SearchMasterMsgMapper;
import com.whl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;


    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        if(employee.getShop_id() != null){ // 说明是店铺人员
            // 根据员工id获取店铺对象
            Shop shop = shopMapper.findOne(employee.getShop_id());
            // 如果员工id不等于店铺管理员id - 是店员
            if(employee.getId() != shop.getAdmin_id()){ // 说明是店员
                query.setEmpId(employee.getId());
            }
            // employee中shop_id不为null，通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员
            query.setShopId(shop.getId());
        }
        // 平台管理员
        return super.queryPage(query);
    }

    /**
     * 确认订单
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        Integer paytype = orderDto.getPaytype();
        BigDecimal money = orderDto.getMoney();
        // 1.修改订单：state=1，money，paytype
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderId);
        order.setState(1);
        order.setPaytype(paytype);
        order.setPrice(money);
        orderPetAcquisitionMapper.update(order);

        // 2.修改寻主消息：state=2，note
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("处理完成，交易成功");
        searchMasterMsgMapper.update(msg);

        // 3.宠物
        // 修改一个成本价 = money
        Pet pet = petMapper.findOne(order.getPetId());
        pet.setCostprice(money);
        petMapper.update(pet);

    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void delete(Long id) {
        // 根据订单id查询订单对象
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        // 将寻主消息中shopId置空 - 放到寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        // 删除宠物 - 没有成功收购
        petMapper.delete(order.getPetId());
        // 删除订单
        super.delete(id);
    }
}
